package net.sxnic.taglib;

import java.io.IOException;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import net.sxnic.comm.CommConstant;
import net.sxnic.util.StrUtils;

import org.apache.commons.lang.StringUtils;

/**
 * 用来直接生成Html的select Html代码
 * 
 * @author 孙宇飞 create date : 2011-4-18
 */
public class BaseCodeCheckboxTag extends BodyTagSupport {

	private static final long serialVersionUID = 1L;

	// 类别编码
	private String sortCode;
	// 在jsp面上显示的名字
	private String name;
	// 预设值 用于回显
	private String value;
	
	private String type;
	private String divWidth;//div总宽度
	private String perDivWidth;//每个小div的宽度
	private String splitLen;//截取文字长度
	private String splitStr = "…";//截取后文字，默认“…”

	public int doStartTag() throws JspException {

		return EVAL_BODY_INCLUDE;
	}

	public int doEndTag() {
		JspWriter out = pageContext.getOut();
		Map<String, String> map = CommConstant.BASECODE_MAP.get(sortCode);
		try {

			if (map == null || map.size() <= 0) {
				out.print("null map");
				return EVAL_PAGE;
			}

			if (StringUtils.isBlank(name)) {
				out.print("error select name");
				return EVAL_PAGE;
			}
			StringBuffer sb = new StringBuffer();
		 
			if(StringUtils.isBlank(divWidth)) {
				divWidth = "500";
			}
			if(StringUtils.isBlank(perDivWidth)) {
				perDivWidth = "150";
			}
			if("div".equalsIgnoreCase(type)) {
				sb.append("<div style=\"width:"+divWidth+"px;\">\n");
			}
			String temp = "";
			for (String code : map.keySet()) {
				if("div".equalsIgnoreCase(type)) {
					sb.append("<div class=\"float_left\" style=\"width:"+perDivWidth+"px;\" ");
				}
				temp = map.get(code);
				if(StringUtils.isNotBlank(splitLen)) {
					sb.append("title=\""+temp+"\" >");
					temp = StrUtils.getCn(temp, Integer.parseInt(splitLen) ,splitStr);
				} else {
					sb.append(">");
				}
				if (StringUtils.contains(value, code)) {
					sb.append("<input type='checkbox' name='" + name
							+ "' value='" + code + "' checked='true' />&nbsp;"
							+ temp + "&nbsp;");
				} else {
					sb.append("<input type='checkbox' name='" + name
							+ "' value='" + code + "' />&nbsp;" + temp
							+ "&nbsp;");
				}
				if("div".equalsIgnoreCase(type)) {
					sb.append("</div>");
				}				
			}
			if("div".equalsIgnoreCase(type)) {
				sb.append("\n</div>");
			}
			 

			out.print(sb.toString());

		} catch (IOException e) {
			e.printStackTrace();
		}

		return EVAL_PAGE;
	}

	public String getSortCode() {
		return sortCode;
	}

	public void setSortCode(String sortCode) {
		this.sortCode = sortCode;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getDivWidth() {
		return divWidth;
	}

	public void setDivWidth(String divWidth) {
		this.divWidth = divWidth;
	}

	public String getPerDivWidth() {
		return perDivWidth;
	}

	public void setPerDivWidth(String perDivWidth) {
		this.perDivWidth = perDivWidth;
	}

	public String getSplitLen() {
		return splitLen;
	}

	public void setSplitLen(String splitLen) {
		this.splitLen = splitLen;
	}

	public String getSplitStr() {
		return splitStr;
	}

	public void setSplitStr(String splitStr) {
		this.splitStr = splitStr;
	}

}
